/**
 * @Author:  renyc
 * @Date: 2022年12月2日14:00:27
 */

import * as Cesium from 'cesium/Cesium';
import Viewer from "../viewer/Viewer";
import Event from './Event';
import {LayerEventType, MouseEventType, OverlayEventType} from "./EventType";
import Transformer from '../utils/Transformer';
import {OverlayPickEvent} from './LayerEvent';

class MouseEvent extends Event{

    constructor(viewer){
        super();
        this._viewer = viewer;
        this._handeler = undefined;         //Cesium.ScreenSpaceEventHandler
        this._actions = {};
        //
        this._setInputAction();
        this._bindActionListeners();
    }

    /**
     * 在Cesium的ScreenSpaceEventHandler上，绑定鼠标事件
     * setInput的action有5类
     * 1）ScreenSpaceEventHandler.PositionedEventCallback 
     * 2）ScreenSpaceEventHandler.MotionEventCallback 
     * 3）ScreenSpaceEventHandler.WheelEventCallback 
     * 4）ScreenSpaceEventHandler.TwoPointEventCallback 
     * 5）ScreenSpaceEventHandler.TwoPointMotionEventCallback
     */
    // _setInputAction(){
    //     let handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
    //     //遍历 Cesium.ScreenSpaceEventType事件类型，为每一类型的事件创建一个Cesium.Event对象
    //     Object.keys(Cesium.ScreenSpaceEventType).forEach(key => {
    //         let type = Cesium.ScreenSpaceEventType[key];
    //         this._actions[type] = new Cesium.Event();
    //         this._cache[type] = new Cesium.Event();
    //         handler.setInputAction(evt => {
    //             this._actions[type].raiseEvent(evt);
    //             //this._actions[type](evt);
    //         }, type);
    //     });       

    //     this._handeler = handler;
    // }

    _setInputAction(){
        let handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
        //遍历 Cesium.ScreenSpaceEventType事件类型，为每一类型的事件创建一个Cesium.Event对象
        Object.keys(MouseEventType).forEach(key => {
            let type = MouseEventType[key];
            this._actions[type] = new Cesium.Event();
            this._cache[type] = new Cesium.Event();
            handler.setInputAction(evt => {
                this._actions[type].raiseEvent(evt);
                //this._actions[type](evt);
            }, type);
        });       

        this._handeler = handler;
    }

    /**
     * 绑定内部Event的监听器
     */
    _bindActionListeners(){
        this._actions[Cesium.ScreenSpaceEventType.LEFT_DOWN].addEventListener(this._onLeftClick, this);
        this._actions[Cesium.ScreenSpaceEventType.LEFT_DOWN].addEventListener(this._onLeftDown, this);
        this._actions[Cesium.ScreenSpaceEventType.LEFT_UP].addEventListener(this._onLeftup, this);
        this._actions[Cesium.ScreenSpaceEventType.LEFT_CLICK].addEventListener(this._onLeftClick, this);
        this._actions[Cesium.ScreenSpaceEventType.RIGHT_DOWN].addEventListener(this._onRightDown, this);
        this._actions[Cesium.ScreenSpaceEventType.RIGHT_UP].addEventListener(this._onRightup, this);
        this._actions[Cesium.ScreenSpaceEventType.RIGHT_CLICK].addEventListener(this._onRightClick, this);
        this._actions[Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK].addEventListener(this._onLeftDblick, this);
        this._actions[Cesium.ScreenSpaceEventType.MOUSE_MOVE].addEventListener(this._onMouseMove, this);
        this._actions[Cesium.ScreenSpaceEventType.WHEEL].addEventListener(this._onWheel, this);

        //this._actions[MouseEventType.PICK].addEventListener(this._onPick, this);
    }

    _onLeftDown(evt){
        console.log("_onLeftDown");
    }
    _onLeftup(evt){
        console.log("_onLeftup");
    }
    _onLeftClick(evt){
        // 触发鼠标左键响应
        this._raiseLeftClickEvent(evt);

        this._raisePickEvent(evt);
        // let position = Transformer.screen2Geographic(evt.position, this._viewer);
        // if(position){
        //     let e = {
        //         geographic : position
        //     };
        //     this._cache[MouseEventType.CLICK].raiseEvent(e);
        // };        
    }
    _onRightDown(evt){
        console.log("_onRightDown");
    }
    _onRightup(evt){
        console.log("_onRightup");
    }
    _onRightClick(evt){
        console.log("_onRightClick");
    }
    _onLeftDblick(evt){
        console.log("_onLeftDblick");
    }

    /**
     * 鼠标移动事件的响应函数
     * @param {} evt 
     * @returns 返回的事件响应参数格式
     * 
     */
    _onMouseMove(evt){
        //屏幕坐标转换为地理坐标
        let position = Transformer.screen2Geographic(evt.endPosition, this._viewer);
        if(position){
            let e = {
                geographic : position        
            };
            this._cache[MouseEventType.MOUSE_MOVE].raiseEvent(e);
        }       
    }

    _onWheel(evt){
        console.log("_onWheel");
    }

    /**
     * 鼠标拾取Overlay
     * @param {} evt 
     */
    _raisePickEvent(evt){
        var instance = this._viewer.scene.pick(evt.position);
          if (Cesium.defined(instance)) {
            //3. 显示
            //alert(instance.id);
            if(instance.id){
                let overlay_id = this._getOverlayId(instance);
                if(overlay_id){
                    let layers = this._viewer.layers;
                    layers.forEach(layer=>{
                        let overlay = layer.getOverlayById(overlay_id);
                        if(overlay){
                            //alert(overlay.id);                    
                            let evt = { //OverlayPickEvent
                                obj : overlay,
                                layer : layer
                            }
                            layer._fire(LayerEventType.PICK, evt);
                            // 触发Overlay的CLICK事件响应
                            overlay._fire(OverlayEventType.CLICK, {
                                obj : overlay
                            });
                        }
                    });
                }                
            }
          }
    }

    /**
     * 触发鼠标左键点击事件
     * @param {} evt 
     */
    _raiseLeftClickEvent(evt){
        let position = Transformer.screen2Geographic(evt.position, this._viewer);
        if(position){
            let e = {
                geographic : position
            };
            this._cache[MouseEventType.CLICK].raiseEvent(e);
        };     
    }

    /**
     * 获取Overlay的ID，Engity和Primitive类型不一样。
     * @param {*} instance 
     * @returns 
     */
    _getOverlayId(instance){
        let overlay_id = undefined;
        if(instance.id instanceof Cesium.Entity){
            overlay_id = instance.id.id;
        }
        else{
            overlay_id = instance.id;
        }
        return overlay_id;
    }
}

export default MouseEvent
